package com.service;

import com.bean.LoginCreds;
import com.bean.WebSiteInfo;
import com.bean.base.WebSite;
import com.bean.base.WebSiteItem;
import com.bean.other.MetaInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.LoginCredsMapper;
import com.mapper.WebSiteInfoMapper;
import com.mapper.repository.LoginCredsRepository;
import com.mapper.repository.WebSiteInfoRepository;
import com.mapper.repository.WebSiteItemRepository;
import com.mapper.repository.WebSiteRepository;
import com.service.impl.LoginCredsService;
import com.service.impl.WebSiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class WebSiteInfoServiceImpl implements WebSiteInfoService {

    @Autowired
    private WebSiteInfoRepository webSiteInfoRepository;

    @Autowired
    private WebSiteInfoMapper webSiteInfoMapper;

    @Override
    public String insert(WebSiteInfo webSiteInfo) {
        webSiteInfo.setId(UUID.randomUUID().toString());
        webSiteInfo.setCreatetime(LocalDateTime.now());
        webSiteInfoRepository.save(webSiteInfo);
        return "新增成功";
    }

    @Override
    public String update(WebSiteInfo webSiteInfo) {
        webSiteInfo.setUpdatetime(LocalDateTime.now());
        webSiteInfoRepository.save(webSiteInfo);
        return "更新成功";
    }

    @Override
    public String delete(WebSiteInfo webSiteInfo) {
        webSiteInfoRepository.delete(webSiteInfo);
        return "删除成功";
    }

    @Override
    public List<WebSiteInfo> select(WebSiteInfo webSiteInfo, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<WebSiteInfo> list=webSiteInfoMapper.selectLike(webSiteInfo);
        //PageInfo<LoginCreds> pageInfo=new PageInfo<>(list);
        return list;
    }

    @Override
    public WebSiteInfo get(String id) {
        Optional<WebSiteInfo> optional=webSiteInfoRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    //====================新的网页管理的内容=====================

    @Autowired
    private WebSiteRepository webSiteRepository;

    @Autowired
    private WebSiteItemRepository webSiteItemRepository;

    @Override
    public String addWebSite(WebSite webSite) {

        if(webSite.getId()==null){
            //新增
            if(webSiteRepository.countByLabel(webSite.getLabel())>0){
                return "Label已存在";
            }
            if(webSiteRepository.countByValue(webSite.getValue())>0){
                return "Value已存在";
            }
            int count = (int) webSiteRepository.count();
            webSite.setId(UUID.randomUUID().toString());
            webSite.setCreatetime(LocalDateTime.now());
            webSite.setNum(count);
        }else{
            //修改
            if(webSiteRepository.countByLabelAndIdIsNot(webSite.getLabel(),webSite.getId())>0){
                return "Label已存在";
            }
            if(webSiteRepository.countByValueAndIdIsNot(webSite.getValue(),webSite.getId())>0){
                return "Value已存在";
            }
            webSite.setUpdatetime(LocalDateTime.now());
        }
        webSiteRepository.save(webSite);
        return "成功";//更新或新增成功
    }

    @Override
    public String delWebSite(WebSite webSite) {
        webSiteRepository.deleteById(webSite.getId());
        return "成功";
    }

    @Override
    public WebSite getWebSite(String id) {
        Optional<WebSite> optional = webSiteRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public List<WebSite> selectWebSite(WebSite webSite) {

        return webSiteInfoMapper.selectWebSite(webSite);
    }

//==========================

    @Override
    public String addWebSiteItem(WebSiteItem webSiteItem) {
        if(webSiteItem.getId()==null){
            //新增
            if(webSiteItemRepository.countByWebsite(webSiteItem.getWebsite())>0){
                return "WebSite已存在";
            }
            if(webSiteItemRepository.countByUrl(webSiteItem.getUrl())>0){
                return "Url已存在";
            }
            webSiteItem.setId(UUID.randomUUID().toString());
            webSiteItem.setCreatetime(LocalDateTime.now());
        }else{
            //修改
            if(webSiteItemRepository.countByWebsiteAndIdIsNot(webSiteItem.getWebsite(),webSiteItem.getId())>0){
                return "Label已存在";
            }
            if(webSiteItemRepository.countByUrlAndIdIsNot(webSiteItem.getUrl(),webSiteItem.getId())>0){
                return "Value已存在";
            }
            webSiteItem.setUpdatetime(LocalDateTime.now());
        }
        webSiteItemRepository.save(webSiteItem);
        return "成功";//更新或新增成功
    }

    @Override
    public String delWebSiteItem(WebSiteItem webSiteItem) {
        webSiteItemRepository.deleteById(webSiteItem.getId());
        return "成功";
    }

    @Override
    public List<WebSiteItem> selectWebSiteItem(WebSiteItem webSiteItem) {

        return webSiteInfoMapper.selectWebSiteItem(webSiteItem);
    }

    @Override
    public WebSiteItem getWebSiteItem(String id) {
        Optional<WebSiteItem> optional = webSiteItemRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public List webSiteItemShow() {
        return null;
    }
}
